﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class Bom : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chbBomOlny;
        protected CheckBox chbIsActive;
        protected HtmlForm F2121;
        protected GridView gvBomDetail;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnSeqNo;
        protected HtmlInputHidden hdnSkuID;
        protected HtmlImage imgOwnerID;
        protected HtmlImage imgSkuID;
        protected Label lblArea;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblRemark;
        protected Label lblSkuID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtOwnerID;
        protected TextBox txtRemark;
        protected TextBox txtSkuID;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2270_002");
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2270_003");
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnSkuID.Value))
                {
                    if (this.InsertBomInfo())
                    {
                        this.hdnSeqNo.Value = "10";
                    }
                    else
                    {
                        return;
                    }
                }
                base.Response.Redirect(string.Format("BomDetail.aspx?from={0}&ID={1}&SeqNO={2}&SkuID={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.hdnSeqNo.Value), base.UrlEncode(this.txtSkuID.Text.Trim()) }), true);
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtOwnerID.Text = "";
            this.txtOwnerID.Enabled = true;
            this.txtSkuID.Text = "";
            this.txtSkuID.Enabled = true;
            this.imgOwnerID.Visible = true;
            this.imgSkuID.Visible = true;
            this.hdnFlag.Value = "0";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()) && !string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                try
                {
                    List<string> OwnerID = new List<string>();
                    List<string> SkuID = new List<string>();
                    List<string> pk3 = new List<string> {
                        this.txtOwnerID.Text.Trim(),
                        this.txtSkuID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(OwnerID, SkuID, pk3, "BOM") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()) && !string.IsNullOrEmpty(this.txtSkuID.Text.Trim())) && (this.hdnFlag.Value == "1"))
            {
                List<string> OwnerID = base.GetSelectedIDsInGridView(this.gvBomDetail, "OwnerID");
                List<string> SkuID = base.GetSelectedIDsInGridView(this.gvBomDetail, "SkuID");
                List<string> ComponentSkuID = base.GetSelectedIDsInGridView(this.gvBomDetail, "ComponentSkuID");
                if (OwnerID.Count > 0)
                {
                    try
                    {
                        this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(OwnerID, SkuID, ComponentSkuID, "BOM_DETAIL") + base.GetGlobalMsgResource("C006");
                        this.SetBomInfo2Form();
                    }
                    catch (Exception ex)
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("C012") + ex.Message;
                        return;
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2270_004");
                return;
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("BomQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("BomList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("F2270_002"));
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("F2270_003"));
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateBomInfo();
            }
            else
            {
                this.InsertBomInfo();
            }
        }

        protected string FormatReceiptUrl(string url, string SeqNo, string OwnerID, string SkuID, string ComponentSkuID, string QTY)
        {
            return string.Format(url, new object[] { this.hdnFrom.Value.Trim(), SeqNo, OwnerID, SkuID, ComponentSkuID, QTY });
        }

        protected void gvBomDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2121", this.gvBomDetail);
        }

        protected void gvBomDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as BomInfo).BomDetailInfo;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvBomDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvBomDetail.PageIndex * this.gvBomDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvBomDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                BomInfo bomInfo = base.DataEntityViewState as BomInfo;
                DataView dv = CADataConverter.ToDataTable(bomInfo.BomDetailInfo).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvBomDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private bool InsertBomInfo()
        {
            bool value = false;
            try
            {
                BomInfo bom = new BomInfo();
                if (base.DataEntityViewState != null)
                {
                    bom = base.DataEntityViewState as BomInfo;
                    if ((bom.BomDetailInfo != null) && (bom.BomDetailInfo.Count > 0))
                    {
                        for (int i = 0; i < bom.BomDetailInfo.Count; i++)
                        {
                            bom.BomDetailInfo[i].OwnerID = this.txtOwnerID.Text.Trim();
                            bom.BomDetailInfo[i].SkuID = this.txtSkuID.Text.Trim();
                        }
                    }
                }
                bom.OwnerID = this.txtOwnerID.Text.Trim();
                bom.SkuID = this.txtSkuID.Text.Trim();
                bom.Remark = this.txtRemark.Text.Trim();
                bom.BomOnly = this.chbBomOlny.Checked ? "Y" : "N";
                bom.IsActive = this.chbIsActive.Checked ? "Y" : "N";
                bom.CreatedBy = base.UserInfo.UserID;
                base.ShowMsgLabel(this.lblMsg, BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(bom, "BOM") + base.GetGlobalMsgResource("C004"));
                value = true;
                this.hdnSkuID.Value = this.lblMsg.Text;
                base.DataEntityViewState = bom;
                this.hdnFlag.Value = "1";
                this.txtOwnerID.Enabled = false;
                this.txtRemark.Enabled = false;
                if ((bom.BomDetailInfo != null) && (bom.BomDetailInfo.Count > 0))
                {
                    this.hdnSeqNo.Value = (int.Parse(bom.BomDetailInfo[bom.BomDetailInfo.Count - 1].SeqNo) + 10).ToString();
                    return value;
                }
                this.hdnSeqNo.Value = "10";
            }
            catch (Exception ex)
            {
                value = false;
                base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("C005") + ex.Message);
            }
            return value;
        }

        private void LoadData()
        {
            if (!(string.IsNullOrEmpty(base.Request.QueryString["ID"]) || string.IsNullOrEmpty(base.Request.QueryString["SkuID"])))
            {
                this.hdnOwnerID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnSkuID.Value = base.Request.QueryString["SkuID"].ToString();
                this.SetBomInfo2Form();
                this.hdnFlag.Value = "1";
            }
            else
            {
                base.SetDefaultOwner(this.txtOwnerID);
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetBomInfo2Form()
        {
            try
            {
                string OwnerID = this.hdnOwnerID.Value;
                string SkuID = this.hdnSkuID.Value;
                BomInfo bom = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(OwnerID, SkuID, "", "BOM") as BomInfo;
                base.DataEntityViewState = bom;
                if (bom != null)
                {
                    this.txtOwnerID.Text = bom.OwnerID;
                    this.txtSkuID.Text = bom.SkuID;
                    this.txtRemark.Text = bom.Remark;
                    this.chbBomOlny.Checked = bom.BomOnly == "Y";
                    this.chbIsActive.Checked = bom.IsActive == "Y";
                    if ((bom.BomDetailInfo != null) && (bom.BomDetailInfo.Count > 0))
                    {
                        this.hdnSeqNo.Value = (int.Parse(bom.BomDetailInfo[bom.BomDetailInfo.Count - 1].SeqNo) + 10).ToString();
                    }
                    else
                    {
                        this.hdnSeqNo.Value = "10";
                    }
                    base.BindData2GridView(this.gvBomDetail, bom.BomDetailInfo);
                    this.txtOwnerID.Enabled = false;
                    this.txtSkuID.Enabled = false;
                    this.imgOwnerID.Visible = false;
                    this.imgSkuID.Visible = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateBomInfo()
        {
            try
            {
                BomInfo bom = base.DataEntityViewState as BomInfo;
                bom.Remark = this.txtRemark.Text.Trim();
                bom.BomOnly = this.chbBomOlny.Checked ? "Y" : "N";
                bom.IsActive = this.chbIsActive.Checked ? "Y" : "N";
                bom.UpdatedBy = base.UserInfo.UserID;
                base.ShowMsgLabel(this.lblMsg, BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(bom, "BOM") + base.GetGlobalMsgResource("C002"));
            }
            catch (Exception ex)
            {
                base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("C003") + ex.Message);
            }
        }
    }
}

